package com.greateamd.timetableasy.client.mvc.view.crud.campus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.greateamd.timetableasy.client.mvc.model.Model;
import com.greateamd.timetableasy.client.services.campus.CampusService;
import com.greateamd.timetableasy.client.services.campus.CampusServiceAsync;
import com.greateamd.timetableasy.client.services.util.ServerCall;
import com.greateamd.timetableasy.client.widgets.EntityContainer;
import com.greateamd.timetableasy.client.widgets.room.ListRoomBuilder;
import com.greateamd.timetableasy.client.widgets.university.UniversityListBox;
import com.greateamd.timetableasy.client.widgets.user.CampusManagerListBox;
import com.greateamd.timetableasy.domain.Campus;
import com.greateamd.timetableasy.domain.Room;
import com.greateamd.timetableasy.domain.User;

public class AddCampusPage <M extends Model> extends IAddCampusPage<M>{

	private final CampusServiceAsync campusService = GWT.create(CampusService.class);
	
	@UiTemplate("AddCampusPage.ui.xml")
	interface AddCampusPageUIBinder extends UiBinder<Widget, AddCampusPage> {}
	private static AddCampusPageUIBinder uiBinder = GWT.create(AddCampusPageUIBinder.class);
	
	@UiField TextBox name;
	@UiField TextBox address;
	//University
	@UiField UniversityListBox listOfUniversities;
	//Rooms
	@UiField ListRoomBuilder listOfRooms;
	//Managers
	@UiField CampusManagerListBox listAllManagers;
	@UiField EntityContainer<User> listCampusManager;
	@UiField Button addManagerBt;
	@UiField Button rmManagerBt;
	
	@UiField Button submitBt;
	
	/**
	 * Constructor
	 */
	public AddCampusPage() {
	    initWidget(uiBinder.createAndBindUi(this));
	}
	
	@Override
	public Widget getWidgetPage() {
		return this;
	}

	@Override
	public void setUser(User user) {
	}


	//---------------------------------------------------------------------
	//-------------------------- UI action

	
	//---------------------------------------------------------------------
	//-------------------------- Handler
	@UiHandler("addManagerBt")
	void onAddAuthBtClick(ClickEvent e){
		if(listAllManagers.getSelectedIndex() != -1){
			listCampusManager.addEntity(listAllManagers.getSelectedUser());
		}
	}
	@UiHandler("rmManagerBt")
	void onRmAuthBtClick(ClickEvent e){
		listCampusManager.removeSelectedEntity();
	}
	@UiHandler("submitBt")
	void onSubmitBtClick(ClickEvent e){
		addCampus();
	}
	
	//---------------------------------------------------------------------
	//-------------------------- Server call
	
	private void addCampus() {
		new ServerCall<Boolean>() {

			@Override
			protected void callService(AsyncCallback<Boolean> asyncCallback) {
				Campus c = new Campus(name.getText(), address.getText());
				for(User manager : listCampusManager.getEntities()){
					c.addManager(manager);
				}
				for(Room room : listOfRooms.getListOfRooms()){
					room.setCampus(c);
					c.addRoom(room);
				}
				c.setUniversity(listOfUniversities.getSelectedUniversity());
				campusService.save(c,asyncCallback);
			}

			@Override
			public void onSuccess(Boolean success) {
				Window.alert("Success " +success);
				//TODO: Could return to a menu
			}
		}.call();
	}
}
